﻿using System;
using System.Data;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

using BLL;
using Model;
using Utility;

namespace EvaluationSystem
{
    public partial class ManageActivity : System.Web.UI.Page
    {
        private string username = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            ((SiteEnterpriseMaster)Master).InitMenu();
            ((SiteEnterpriseMaster)Master).li1.Attributes.Add("class", "hover ccc");            
            ((SiteEnterpriseMaster)Master).b_hmenu1.Style.Clear();
            ((SiteEnterpriseMaster)Master).b_hmenu2.Style.Add("display", "none");
            ((SiteEnterpriseMaster)Master).b_hmenu3.Style.Add("display", "none");
            ((SiteEnterpriseMaster)Master).b_hmenu4.Style.Add("display", "none");
            ((SiteEnterpriseMaster)Master).s_menu1_1.Style.Add("COLOR", "#147ab8");
            ((SiteEnterpriseMaster)Master).s_menu1_1.Style.Add("TEXT-DECORATION", "none");

            if (Request.QueryString["UID"] == null)//普通企业用户输入用户名和密码登录
            {
                /*判断是否是企业用户身份*/
                if (Session["username"] == null)
                {
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "PopupScript", "<script>alert('请先登录系统!');top.location.href='../Enterprise/EnterpriseLogin.aspx';</script>");
                    return;
                }
                else
                {
                    username = Session["username"].ToString();
                    bind();
                }
            }
            else//从系统端直接跳转过来2012.3.13
            {
                string uID = Request.QueryString["UID"].ToString();
                Session["EuserID"] = uID;
                EnterpriseUserBLL euserbll = new EnterpriseUserBLL();
                username = euserbll.GetModel(Int32.Parse(uID)).Username;
                Session["username"] = username;
                bind();
            }
        }

        private void bind()
        {
            if (string.IsNullOrEmpty(username)) { return; }

            ActivityBLL temp = new ActivityBLL();

            DataSet ds = temp.GetActivityByUser(username);

            grVActivityManage.DataSource = ds.Tables[0].DefaultView;
            grVActivityManage.DataSourceID = null;
            grVActivityManage.DataBind();
            temp = null;
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
        }

        protected void GrVActivityManage_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            int rowIndex = Int32.Parse(e.CommandArgument.ToString());//获取点击了第几行

            if (e.CommandName == "GuidSetting")
            {
                string guidNew = GenerateGuid.NewGuidNo().Substring(0, 8);
                GuidNo guidObject = new GuidNo();
                guidObject.GuidStr = guidNew;
                guidObject.ActivityId = int.Parse(grVActivityManage.DataKeys[rowIndex].Values[0].ToString());
                guidObject.CreateTime = DateTime.Now;
                GuidBLL temp = new GuidBLL();
                if (temp.Add(guidObject) != -1)
                {
                    MessageBox.ShowAndRedirect(this, "已成功开启项目", "ManageActivity.aspx");
                }
            }
            if (e.CommandName == "DisableActivity")
            {
                string strStatus = ((Button)grVActivityManage.Rows[rowIndex].Cells[8].Controls[0]).Text;
                string actid = grVActivityManage.DataKeys[rowIndex].Values[0].ToString();
                string positonid = grVActivityManage.DataKeys[rowIndex].Values[1].ToString();
                EnterpriseUserBLL enterprisebll = new EnterpriseUserBLL();
                string enterpriseid = enterprisebll.GetEnterpriseIdByUser(Session["username"].ToString());
                ActivityBLL temp = new ActivityBLL();
                switch (strStatus)
                {
                    case "开启项目":
                        if (temp.UpdateActivityStatus(actid, positonid, enterpriseid, false) != -1)
                        {
                            MessageBox.ShowAndRedirect(this, "已成功开启项目", "ManageActivity.aspx");
                        }
                        break;
                    case "停止项目":
                        if (temp.UpdateActivityStatus(actid, positonid, enterpriseid, true) != -1)
                        {
                            MessageBox.ShowAndRedirect(this, "已成功停止项目", "ManageActivity.aspx");
                        }
                        break;
                    default:
                        break;
                }
                temp = null;
            }
        }

    }
}